package tech.dingxin.codec;

import org.opencv.core.Mat;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.*;
import java.util.Iterator;

public class Image {

    private byte[] data;

    public Image() throws IOException {
        File f = new File("A:\\nrvs\\src\\main\\resources\\1.jpg");
        FileInputStream fis = new FileInputStream(f);
        data = new byte[fis.available()];
        fis.read(data);
//        System.out.println(data.length);
//        double time = System.currentTimeMillis();
//        data = compressPicture(data, 0.1F);
//        System.out.println(System.currentTimeMillis() - time);
//        System.out.println(data.length);
    }

    public Image(String filename) throws IOException {
        File f = new File(filename);
        FileInputStream fis = new FileInputStream(f);
        data = new byte[fis.available()];
        fis.read(data);
    }
    /**
     * 图片压缩质量然后返回一个byte数组
     *
     * @param imageByte 图片byte数组
     * @param quality   压缩质量，在0-1之间，
     * @return 返回的字节数组
     */
    public static byte[] compressPicture(byte[] imageByte, float quality) throws IOException {
        //将byte数组转换成byte输入流
        ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte);
        //将输入流，变成图片流
        BufferedImage image = ImageIO.read(byteInput);
        // 如果图片空，返回空
        if (image == null) {
            return null;
        }
        // 得到指定Format图片的writer
        // 得到迭代器
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        // 得到writer
        ImageWriter writer = (ImageWriter) iter.next();
        // 得到指定writer的输出参数设置(ImageWriteParam )
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        // 设置可否压缩
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        // 设置压缩质量参数
        iwp.setCompressionQuality(quality);
        iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
        ColorModel colorModel = ColorModel.getRGBdefault();
        // 指定压缩时使用的色彩模式
        iwp.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel, colorModel.createCompatibleSampleModel(16, 16)));
        // 开始打包图片，写入byte[]
        // 取得内存输出流
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        IIOImage iIamge = new IIOImage(image, null, null);
        try {
            // 此处因为ImageWriter中用来接收write信息的output要求必须是ImageOutput
            // 通过ImageIo中的静态方法，得到byteArrayOutputStream的ImageOutput
            writer.setOutput(ImageIO.createImageOutputStream(byteArrayOutputStream));
            writer.write(null, iIamge, iwp);
        } catch (IOException e) {
            return null;
        } finally {
            byteInput.close();
            byteArrayOutputStream.close();
        }
        return byteArrayOutputStream.toByteArray();
    }

    public byte[] getData() {
        return data;
    }
}
